﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DAO;

namespace TAM_SPORT
{
    public partial class ProductList : TAM_SPORT.FormBase
    {
        ProductDao dao;
        public ProductList()
        {
            InitializeComponent();
            dao = new ProductDao();
            init();
        }
        private void init()
        {
            DataTable table = dao.getProducts(txtName.Text.Trim());
            dtgCustomers.Rows.Clear();
            if (table != null && table.Rows.Count >= 0)
            {
                foreach (DataRow row1 in table.Rows)
                {
                    int i = dtgCustomers.Rows.Add();
                    dtgCustomers.Rows[i].Cells[0].Value = i + 1;
                    dtgCustomers.Rows[i].Cells[1].Value = row1["NAME"].ToString();
                    dtgCustomers.Rows[i].Cells[2].Value = double.Parse(row1["PRICE"].ToString()).ToString("#,####.##");
                    dtgCustomers.Rows[i].Cells[3].Value = row1["UNAME"].ToString();
                    dtgCustomers.Rows[i].Cells[4].Value = row1["ID"].ToString();
                    if (i % 2 == 1)
                    {
                        dtgCustomers.Rows[i].DefaultCellStyle.BackColor = Color.LightCyan;
                    }
                }
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            init();
        }

        
        private void dtgCustomers_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            /*
            if (e.RowIndex >= 0 && e.ColumnIndex == 5)
            {
                int id = int.Parse(dtgCustomers.Rows[e.RowIndex].Cells[4].Value.ToString());
                AddProducts ad = new AddProducts(id);
                ad.ShowDialog();
                init();
            }

            else if (e.RowIndex >= 0 && e.ColumnIndex == 6)
            {
                int id = int.Parse(dtgCustomers.Rows[e.RowIndex].Cells[4].Value.ToString());
                Confirm con = new Confirm("Bạn có chắc muốn xóa  sản phẩm này?");
                con.ShowDialog();
                if (con.IsAccept)
                {
                    if (dao.deleteProduct(id))
                    {
                        Result r = new Result("Xóa thành công.");
                        r.ShowDialog();
                        r.Dispose();
                        init();
                    }
                    else
                    {
                        Error er = new Error("Xóa thất bại.");
                        er.ShowDialog();
                        er.Dispose();
                    }
                }
                con.Dispose();
            }
             * */
        }

        private void dtgCustomers_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex == 5)
            {
                int id = int.Parse(dtgCustomers.Rows[e.RowIndex].Cells[4].Value.ToString());
                AddProducts ad = new AddProducts(id);
                ad.ShowDialog();
                init();
            }

            else if (e.RowIndex >= 0 && e.ColumnIndex == 6)
            {
                int id = int.Parse(dtgCustomers.Rows[e.RowIndex].Cells[4].Value.ToString());
                Confirm con = new Confirm("Bạn có chắc muốn xóa sản phẩm này?");
                con.ShowDialog();
                if (con.IsAccept)
                {
                    if (dao.deleteProduct(id))
                    {
                        Result r = new Result("Xóa thành công.");
                        r.ShowDialog();
                        r.Dispose();
                        init();
                    }
                    else
                    {
                        Error er = new Error("Xóa thất bại.");
                        er.ShowDialog();
                        er.Dispose();
                    }
                }
                con.Dispose();
            }
        }
    }
}
